# -*-coding:utf-8-*-
import re
import json
from utils import read_txt,cut_sent,read_hed
from Syntactic_Analysis import ParseResult
import shi_judge,you_judge,cunxianju_judge,compare_judge,ba_judge,bei_judge,wuzhu_judge,AdjPre_judge,zhuwei_judge
from find_chusuoci import find_chusuoci
import time
'''
输入：已分句的txt文本
输出：输出每个单句的类型,json文件保存句子的相关信息
覆盖大句型：“是”字句，“有”字句，存现句，比较方式，“把”字句，“被”字句，无主句
覆盖细分类句型共46种
已覆盖以上大句型的大部分细分类，除了“有”字表示比较，难以和“有”字句区分
'''
def find_key_idx(fenci_res,key_lst):
    for word in fenci_res:
        if word in key_lst:
            return fenci_res.index(word)
    return -1

if __name__ == '__main__':
    start = time.time()
    path = './data/test.txt'
    sentences = read_txt(path)
    results = {}
    idx = -1
    print(re.findall(r'[\u628a]',"门打开"))
    for sent in sentences:
        idx += 1
        print('*' * 125)
        print(sent)
        fenci_res, pos_res, ddp_res = cut_sent(sent)
        pr = ParseResult(fenci_res, pos_res, ddp_res)
        # print('分词：', fenci_res)
        # print('词性：', pos_res)
        # print('句法：', ddp_res)
        modifi_info = pr.get_modifi_info()
        hed_info = pr.get_hed_info()
        type = "其他句型"
        if find_key_idx(fenci_res, ['比','跟','不如']) > -1 and (type == '其他句型'):                # 比较方式
            type = compare_judge.judge_type(hed_info,modifi_info,fenci_res,pos_res)
        if find_key_idx(fenci_res,["被", "让","叫","给","为"]) > -1 and (type == '其他句型'):         # 被字句
            type = bei_judge.judge_type(hed_info,modifi_info,fenci_res,pos_res)
        if re.findall(r'[\u628a]',sent) and (type == '其他句型'):                                  # 把字句
            type = ba_judge.judge_type(hed_info,modifi_info,fenci_res,pos_res)
        if re.findall(r'[\u662f]',sent) and (type == '其他句型'):                                   # 是字句
            type = shi_judge.judge_type(hed_info,modifi_info)
        if re.findall(r'[\u6709]',sent) and (type == '其他句型'):                                   # 有字句
            type = you_judge.judge_type(hed_info,modifi_info)
        if find_chusuoci(sent) > -1 and (type == '其他句型'):  # 存现句
            type = cunxianju_judge.judge_type(hed_info, modifi_info)
        if read_hed(modifi_info['sbv_info'], hed_info['word']) == {} and (type == '其他句型'):      # 无主句
            type = wuzhu_judge.judge_type(hed_info, modifi_info)
        if type == "其他句型":
            type = AdjPre_judge.judge_type(hed_info,modifi_info,fenci_res,pos_res)
        if type == "其他句型":
            type = zhuwei_judge.judge_type(hed_info,modifi_info,fenci_res,pos_res)
        print(type)
        results[idx] = {'sentence':sent, 'sent_type':type,
                        'fenci_res':fenci_res, 'pos_res':pos_res,'ddp_res':ddp_res}
    with open('./Results-7.3.json', 'w', encoding='utf8') as f:
        json.dump(results, f, ensure_ascii=False)
    print(time.time()-start)